<template>
  <div class="orderlist">
    <el-dialog
      :title="state ? '编辑订单' : '添加订单'"
      :visible.sync="dialogFormVisible"
    >
      <el-form
        :model="submitData"
        status-icon
        :label-position="labelPosition"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <div class="flex-col-12">
          <el-form-item label="收货人" prop="buyerName" required>
            <el-input v-model="submitData.buyerName"></el-input>
          </el-form-item>
        </div>
        <div class="flex-col-12">
          <el-form-item label="手机号" prop="phone" required>
            <el-input v-model="submitData.phone"></el-input>
          </el-form-item>
        </div>
        <div class="flex-col-12">
          <el-form-item label="收货地址" prop="buyerAddr" required>
            <el-input v-model="submitData.buyerAddr"></el-input>
          </el-form-item>
        </div>
        <div class="flex-col-12">
          <el-form-item label="商品名称" prop="goodsName" required>
            <el-input v-model="submitData.goodsName" disabled></el-input>
          </el-form-item>
        </div>
        <div class="flex-col-12">
          <el-form-item label="商品规格" prop="goodsOptions" required>
            <el-input v-model="submitData.goodsOptions" disabled></el-input>
          </el-form-item>
        </div>
        <div class="flex-col-12">
          <el-form-item label="商品价格" prop="goodsPrice" required>
            <el-input v-model="submitData.goodsPrice" disabled></el-input>
          </el-form-item>
        </div>
        <div class="flex-col-12">
          <el-form-item label="商品数量" prop="goodsCount" required>
            <el-input v-model="submitData.goodsCount" disabled></el-input>
          </el-form-item>
        </div>
        <div class="flex-col-12">
          <el-form-item label="订单状态" prop="orderState" required>
            <el-select v-model="submitData.orderState" placeholder="订单状态">
              <el-option label="待付款" :value="0"></el-option>
              <el-option label="待发货" :value="1"></el-option>
              <el-option label="待收货" :value="2"></el-option>
              <el-option label="交易成功" :value="3"></el-option>
              <el-option label="交易失败" :value="-1"></el-option>
            </el-select>
          </el-form-item>
        </div>
        <div class="flex-col-12">
          <el-form-item label="状态" prop="status" required>
            <el-select v-model="submitData.status" placeholder="状态">
              <el-option label="已删除" :value="0"></el-option>
              <el-option label="未删除" :value="1"></el-option>
            </el-select>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <!-- <el-button @click="resetForm('ruleForm')">重置</el-button> -->
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
    <div class="inner-container container">
      <div class="filter-container flex-row">
        <div class="flex-col-3 input-box">
          <el-input
            size="small"
            v-model="queryParams.orderId"
            placeholder="订单编号"
          ></el-input>
        </div>
        <div class="flex-col-3 input-box">
          <el-input
            v-model="queryParams.storeName"
            placeholder="店铺名称"
          ></el-input>
        </div>
        <div class="flex-col-3 input-box">
          <el-select v-model="queryParams.status" placeholder="请选择状态">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </div>
        <div class="btn-box">
          <el-button
            type="primary"
            icon="el-icon-search"
            plain
            @click="search"
            v-permission="{ action: 'view', effect: 'disabled' }"
            >搜索</el-button
          >
          <el-button
            type="danger"
            icon="el-icon-delete"
            plain
            v-permission="{ action: 'delete', effect: 'disabled' }"
            @click="batchDelete"
            >批量删除</el-button
          >
        </div>
      </div>
      <el-table
        :data="tableData"
        border
        v-loading="loading"
        ref="multipleTable"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        :header-cell-style="{
          background: 'var(--primary-color)',
          color: 'var(--text-sidebar-hover-color)',
        }"
      >
        <el-table-column align="center" type="selection" width="55">
        </el-table-column>
        <el-table-column align="center" type="index" label="ID" width="66">
        </el-table-column>
        <el-table-column align="center" label="订单编号" prop="orderId">
        </el-table-column>
        <el-table-column align="center" label="收货人" prop="buyerName">
        </el-table-column>
        <el-table-column align="center" label="手机号" prop="phone" width="180">
        </el-table-column>
        <el-table-column
          align="center"
          label="收货地址"
          prop="buyerAddr"
          width="180"
        >
        </el-table-column>
        <el-table-column align="center" label="店铺名称" prop="storeName">
        </el-table-column>
        <el-table-column
          align="center"
          label="商品名称"
          prop="goodsName"
          width="180"
        >
        </el-table-column>
        <el-table-column align="center" label="商品封面" width="120">
          <template slot-scope="scope">
            <img
              style="width: 80px; height: 80px"
              :src="`${$upload_path}/${scope.row.goodsImg}`"
            />
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="商品规格"
          prop="goodsOptions"
          width="180"
        >
        </el-table-column>
        <el-table-column align="center" label="商品数量" prop="goodsCount">
        </el-table-column>
        <el-table-column align="center" label="价格">
          <template slot-scope="scope">
            {{ scope.row.goodsPrice }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="createTime"
          label="创建时间"
          width="180"
        >
        </el-table-column>
        <el-table-column align="center" prop="createUser" label="创建者">
        </el-table-column>
        <el-table-column
          align="center"
          prop="updateTime"
          label="修改时间"
          width="180"
        >
        </el-table-column>
        <el-table-column align="center" prop="updateUser" label="操作者">
        </el-table-column>
        <el-table-column align="center" label="状态">
          <template slot-scope="scope">
            <el-tag type="warning" v-if="scope.row.orderState === 0"
              >待付款</el-tag
            >
            <el-tag type="primary" v-if="scope.row.orderState === 1"
              >待发货</el-tag
            >
            <el-tag type="info" v-if="scope.row.orderState === 2"
              >待收货</el-tag
            >
            <el-tag type="success" v-if="scope.row.orderState === 3"
              >交易成功</el-tag
            >
            <el-tag type="danger" v-if="scope.row.orderState === -1"
              >交易失败</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="180">
          <template slot-scope="scope">
            <el-button
              plain
              type="primary"
              class="edit"
              icon="el-icon-edit"
              v-permission="{ action: 'update', effect: 'disabled' }"
              @click="editData(scope.row)"
              >编辑</el-button
            >
            <el-popconfirm
              title="确定删除吗？"
              @confirm="handleDeleteRow(scope.row.orderId)"
            >
              <el-button
                v-permission="{ action: 'delete', effect: 'disabled' }"
                plain
                icon="el-icon-delete"
                type="danger"
                slot="reference"
                >删除</el-button
              >
            </el-popconfirm>
          </template>
        </el-table-column>
        <template slot="empty">
          <el-empty :image-size="200" description="暂无数据"></el-empty>
        </template>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="queryParams.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { storePageQuery, insert, update, deleteData } from '@/api/order'
import { getTableData, handleVoidMethod } from '@/api/table'
import { upload } from '@/api/upload'
export default {
  name: 'MyBrandList',
  data() {
    return {
      options: [
        {
          value: -1,
          label: '全部',
        },
        {
          value: 0,
          label: '禁用',
        },
        {
          value: 1,
          label: '启用',
        },
      ],
      tableData: [],
      multipleSelection: [],
      queryParams: {
        status: -1,
        page: 1,
        pageSize: 10,
      },
      submitData: {
        status: '',
      },
      rules: {
        name: [{ required: true, message: '请输入品牌名称', trigger: 'blur' }],
      },
      total: 0,
      state: false,
      loading: false,
      imageUrl: '',
      currentPage: 1,
      labelPosition: 'right',
      dialogFormVisible: false,
    }
  },
  created() {
    this.getTableData()
  },
  methods: {
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.queryParams.pageSize = val
      this.getTableData()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.queryParams.page = val
      this.getTableData()
    },
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          this.state
            ? handleVoidMethod(
                this,
                '修改成功',
                '修改失败',
                update(this.submitData),
                this.cancel
              )
            : handleVoidMethod(
                this,
                '添加成功',
                '添加失败',
                insert(this.submitData),
                this.cancel
              )
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = []
      val.forEach((item) => {
        const { orderId } = item
        this.multipleSelection.push(orderId)
      })
    },
    uploadFile(item) {
      this.formData = item.raw // 图片文件
      this.imageUrl = URL.createObjectURL(item.raw) // 图片上传浏览器回显地址
      console.log(this.imageUrl, 'imageUrl')
      console.log(this.formData, 'formData')
    },
    handleRemove() {
      this.imageUrl = ''
      this.formData = null
      console.log(this.formData)
      console.log('QAQ')
    },
    async submitOrder() {
      var formData = new FormData()
      formData.append('file', this.formData) // 照片文件
      const { data: res, code } = await upload(formData)
      if (code === 200) {
        this.$message({ type: 'success', message: '上传成功' })
      } else {
        this.$message({ type: 'error', message: '上传失败' })
      }
      this.submitData.logo = res
    },
    handleDeleteRow(id) {
    
      console.log(id)
      handleVoidMethod(this, '删除成功', '删除失败', deleteData([id]))
    },
    cancel() {
      this.submitData = {}
      this.dialogFormVisible = false
    },
    getTableData() {
      this.loading = true
      getTableData(this, storePageQuery(this.queryParams))
    },
    search() {
      this.getTableData()
    },
    addData() {
      this.state = false
      this.imageUrl = ''
      this.submitData = {}
      this.dialogFormVisible = true
    },
    editData(item) {
      this.state = true
      const brand = JSON.parse(JSON.stringify(item))
      this.imageUrl = brand.logo ? `${this.$upload_path}/${brand.logo}` : ''
      this.submitData = JSON.parse(JSON.stringify(item))
      this.dialogFormVisible = true
    },
    batchDelete() {
      if (this.multipleSelection.length <= 0) {
        return this.$message({ type: 'warning', message: '请选择对象' })
      }
      handleVoidMethod(
        this,
        '删除成功',
        '删除失败',
        deleteData(this.multipleSelection)
      )
    },
  },
}
</script>

<style lang="less" scoped>
.orderlist {
  padding: 10px;
  box-sizing: border-box;
  .filter-container {
    flex-wrap: wrap;
    box-sizing: border-box;
    margin-bottom: 20px;
  }
  .inner-container {
    box-sizing: border-box;
    border-radius: 5px;
    padding: 20px;
    background-color: var(--bg-primary-color);
    transition: var(--tran-01);
  }
  .input-box {
    margin-right: 10px;
  }
}
.el-select {
  width: 100%;
}
/deep/ .el-dialog {
  background: var(--bg-primary-color);
  transition: var(--tran-01);
}
/deep/ .el-dialog__title {
  color: var(--text-normal-color) !important;
  transition: var(--tran-01);
}
/deep/ .el-form-item__label {
  color: var(--text-normal-color) !important;
  transition: var(--tran-01);
}
.el-form {
  display: flex;
  flex-wrap: wrap;
}
/deep/ .el-form-item__label {
  text-align: center !important;
}
.el-pagination {
  margin-top: 20px;
}

/deep/ .el-pagination button {
  background-color: var(--bg-primary-color);
  transition: var(--tran-01);
}
/deep/ .el-pager li {
  background-color: var(--bg-primary-color);
  transition: var(--tran-01);
}
/deep/ .el-pager li.active {
  color: var(--primary-color);
  background-color: var(--bg-primary-color);
  transition: var(--tran-01);
}
/deep/ .el-pagination__total {
  color: var(--text-normal-color);
  transition: var(--tran-01);
}
/deep/ .el-pagination__jump {
  color: var(--text-normal-color);
  transition: var(--tran-01);
}
.btn-box {
  display: flex;
  flex-wrap: wrap;
}
/deep/ .el-button {
  flex-grow: 1;
}
/deep/ .el-input .el-input__inner {
  color: var(--text-normal-color);
  background-color: var(--history-color) !important;
  transition: var(--tran-01);
}

/deep/ .el-select-dropdown {
  background-color: var(--history-color) !important;
}
/deep/ .el-table,
.el-table__expanded-cell {
  background-color: transparent;
}

/deep/ .el-table__body-wrapper.is-scrolling-left::-webkit-scrollbar,
/deep/ .el-table__body-wrapper.is-scrolling-right::-webkit-scrollbar,
/deep/ .el-table__body-wrapper.is-scrolling-middle::-webkit-scrollbar {
  height: 8px;
}
/deep/ .el-table__body-wrapper.is-scrolling-left::-webkit-scrollbar-thumb,
/deep/ .el-table__body-wrapper.is-scrolling-right::-webkit-scrollbar-thumb,
/deep/ .el-table__body-wrapper.is-scrolling-middle::-webkit-scrollbar-thumb {
  background: #ccc; // 滑块颜色
  border-radius: 6px; // 滑块圆角
}
/deep/ .el-table__body-wrapper.is-scrolling-left::-webkit-scrollbar-track,
/deep/ .el-table__body-wrapper.is-scrolling-right::-webkit-scrollbar-track,
/deep/ .el-table__body-wrapper.is-scrolling-middle::-webkit-scrollbar-track {
  border-radius: 8px; // 轨道圆角
  //   background-color: #fff // 轨道颜色 ;
}
/deep/ .el-table tr {
  color: var(--text-normal-color);
  background-color: var(--history-color);
  transition: var(--tran-01);
}

/deep/ .el-textarea__inner {
  color: var(--text-normal-color);
  background-color: var(--history-color);
  transition: var(--tran-01);
}

/deep/ .el-table .cell {
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden; //多出的隐藏
  //text-overflow: ellipsis;//多出部分用...代替
  display: -webkit-box; //定义为盒子模型显示
  -webkit-line-clamp: 2; //用来限制在一个块元素显示的文本的行数
  -webkit-box-orient: vertical; //从上到下垂直排列子元素（设置伸缩盒子的子元素排列方式）
}

/deep/ .el-table__body tr:hover > td {
  background-color: #a69ff5 !important;
  color: var(--text-sidebar-hover-color);
}

/deep/ .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

/deep/ .el-upload-dragger {
  width: 100px;
  height: 100px;
}

.deleteImg {
  font-size: 20px;
  position: absolute;
  top: 0;
  right: 0;
  z-index: 999;
}

img {
  width: 100%;
  height: 100%;
  background-repeat: no-repeat;
}
.upload-wrapper {
  display: flex;
  margin-bottom: 20px;
  .label {
    box-sizing: border-box;
    width: 100px;
    padding: 0 12px 0 0;
    text-align: center;
    color: var(--text-normal-color);
    transition: var(--tran-01);
  }
  .send {
    width: 100px;
    margin-top: 10px;
  }
}

.edit {
  margin-right: 10px;
}
</style>
